using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Reflection;
using System.Collections.Specialized;
using Microsoft.SqlServer.Management.Smo;
using Smo = Microsoft.SqlServer.Management.Smo;

namespace dotSqlCopy
{
    public class DBObject
    {
        private string _objectDef;
        private NamedSmoObject _smoObject;
        private string _objectType;
        private ArrayList _arrayDef = new ArrayList();

        #region Constructors
        public DBObject(NamedSmoObject smoObject)
        {
            _smoObject = smoObject;
            _objectType = _smoObject.GetType().Name;
        }

        #endregion

        #region Public Properties
        public string Name
        {
            get { return _smoObject.Name; }
        }
        public string Type
        {
            get { return _objectType; }
        }
        public string Definition
        {
            get { return _objectDef; }
        }
        public ArrayList DefinationArray
        {
            get { return _arrayDef; }
        }

        private bool _ClusteredIndexes = true;
        public bool ClusteredIndexes
        {
            get { return _ClusteredIndexes; }
            set { _ClusteredIndexes = value; }
        }

        private bool _ExtendedProperties = true;
        public bool ExtendedProperties
        {
            get { return _ExtendedProperties; }
            set { _ExtendedProperties = value; }
        }

        private bool _Indexes = true;
        public bool Indexes
        {
            get { return _Indexes; }
            set { _Indexes = value; }
        }

        private bool _NonClusteredIndexes = true;
        public bool NonClusteredIndexes
        {
            get { return _NonClusteredIndexes; }
            set { _NonClusteredIndexes = value; }
        }

        private bool _Permissions = true;
        public bool Permissions
        {
            get { return _Permissions; }
            set { _Permissions = value; }
        }

        private bool _Triggers = true;
        public bool Triggers
        {
            get { return _Triggers; }
            set { _Triggers = value; }
        }

        #endregion

        public void GenerateScript()
        {
            ScriptObject();
        }

        #region Private Methods
        private void ScriptObject()
        {
            try
            {
                string _filename = @"out.txt";
                _objectDef = string.Empty;
                ScriptingOptions options = new ScriptingOptions();
                options.Encoding = Encoding.Default;
                options.ScriptDrops = false;
                options.IncludeDatabaseContext = false;
                options.IncludeIfNotExists = false;
                options.FileName = _filename;
                options.ClusteredIndexes = _ClusteredIndexes;
                options.ExtendedProperties = _ExtendedProperties;
                options.Indexes = _Indexes;
                options.NonClusteredIndexes = _NonClusteredIndexes;
                options.Permissions = _Permissions;
                options.Triggers = _Triggers;
                options.DriAll = true;
                IScriptable me = (IScriptable)_smoObject;
                me.Script(options);
                using (StreamReader sr = new StreamReader(_filename))
                {
                    String line;
                    string formatLine;
                    int i = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Trim().Length > 0)
                        {
                            i++;
                            //formatLine = string.Format("{0}\t{1}\r\n", i, line);
                            formatLine = string.Format("{1}\r\n", i, line);
                            _objectDef += formatLine;
                            _arrayDef.Add(formatLine);
                        }
                    }

                }
            }
            finally
            {
                if (File.Exists(@"out.txt"))
                {
                    File.Delete(@"out.txt");
                }
            }
        }
        #endregion

    }
}
